﻿using Bll;
using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Myproject.BSM.Ashx.PcBase
{
    /// <summary>
    /// FriendAjax 的摘要说明
    /// </summary>
    public class FriendAjax : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (!string.IsNullOrEmpty(cookieHelp.AdminId))
            {
                if (new AdminBLL().ExistsByAdminName(cookieHelp.AdminName) && new AdminBLL().ExistsByAdminId(cookieHelp.AdminId))
                {
                    string dos = context.Request.Params["dos"];
                    string id;
                    #region swith do
                    switch (dos)
                    {
                        case "delete":
                            id = context.Request.QueryString["id"];
                            if (new FriendBLL().Delete(id))
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "删除成功！", "pc_base_FriendList", "", "", false, "", ""));
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "删除失败！", "pc_base_FriendList", "", "", false, "", ""));
                            }
                            break;

                        case "rank":
                            id = context.Request.QueryString["id"];
                            string rank = context.Request.QueryString["rank"];
                            if (id != "0")
                            {
                                FriendModel model = new FriendBLL().GetModel(int.Parse(id));
                                model.FriendRank += int.Parse(rank);
                                if (new FriendBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改排序成功！", "pc_base_FriendList", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "pc_base_FriendList", "", "", false, "", ""));
                                }
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "pc_base_FriendList", "", "", false, "", ""));
                            }
                            break;

                        case "show":
                            id = context.Request.QueryString["id"];
                            string show = context.Request.QueryString["show"];
                            if (id != "0")
                            {
                                FriendModel model = new FriendBLL().GetModel(int.Parse(id));
                                model.FriendFlag = bool.Parse(show);
                                if (new FriendBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改成功！", "pc_base_FriendList", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！", "pc_base_FriendList", "", "", false, "", ""));
                                }
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！", "pc_base_contactList", "", "", false, "", ""));
                            }
                            break;

                        case "edit":
                            string FriendID = context.Request.Params["pc_base_friendEdit_friendID"].Trim();
                            string FriendName = context.Request.Params["pc_base_friendEdit_name"].Trim();
                            string FriendImgUrl = context.Request.Params["pc_base_friendEdit_imgurl"].Trim();
                            string FriendRank = context.Request.Params["pc_base_friendEdit_frank"].Trim();
                            string FriendUrl = context.Request.Params["pc_base_friendEdit_eurl"].Trim();
                            string FriendFlag = context.Request.Params["pc_base_friendEdit_friendFlag"].Trim();
                            if (FriendID == "0")
                            {
                                if (new FriendBLL().Add(FriendName, FriendImgUrl, Int32.Parse(FriendRank), FriendUrl, bool.Parse(FriendFlag)))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "新增友情链接成功！", "pc_base_FriendList", "", "", true, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "新增友情链接失败！", "pc_base_FriendList", "", "", true, "", ""));
                                }
                            }
                            else
                            {
                                FriendModel model = new FriendBLL().GetModel(Int32.Parse(FriendID));
                                model.FriendName = FriendName;
                                model.FriendImgUrl = FriendImgUrl;
                                model.FriendRank = int.Parse(FriendRank);
                                model.FriendUrl = FriendUrl;
                                model.FriendFlag = bool.Parse(FriendFlag);
                                if (new FriendBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改友情链接成功！", "pc_base_FriendList", "", "", true, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改友情链接失败！", "pc_base_FriendList", "", "", true, "", ""));
                                }
                            }
                            break;
                    }
                    #endregion
                }
                else
                {
                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(301, "登陆超时！", "", "", "", false, "/bsm/logFrom.aspx", "登陆超时！"));
                }
            }
            else
            {
                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(301, "登陆超时！", "", "", "", false, "/bsm/logFrom.aspx", "登陆超时！"));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}